package com.affectu.search;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.hibernate.search.FullTextQuery;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.intercept.BypassInterceptors;

import com.affectu.dao.DAOBase;
import com.affectu.task.InnerTask;
import com.affectu.task.OuterTask;
import com.affectu.task.Task;
import com.affectu.user.User;

/**
 * Oct 11, 2008
 * 
 * @author daniel nathan
 */
@Name("searchAction")
@Scope(ScopeType.SESSION)
@BypassInterceptors
public class SearchAction extends DAOBase implements Serializable {

	private static final long serialVersionUID = -6477802078988127609L;

	@In(required = false, scope = ScopeType.SESSION)
	@Out(required = false, scope = ScopeType.SESSION)
	private String keywords;

	@In(required = false, scope = ScopeType.SESSION)
	@Out(required = false, scope = ScopeType.SESSION)
	private String searchType = "task";

	private String[] taskFields = { "name", "tag", "description" };

	@SuppressWarnings("unchecked")
	public SearchResult<Task> searchTasksBylucene() {
		SearchResult sro = new SearchResult<Task>();
		List<Task> resultList = new ArrayList<Task>();
		FullTextQuery ftQuery = null;
		long startTime = System.currentTimeMillis();
		try {
			ftQuery = this.getFullTextSession().createFullTextQuery(
					LuceneUtil.getInstance().getLuceneQuery(taskFields,
							this.getKeywords()), InnerTask.class,
					OuterTask.class);
			Sort sort = new Sort(new SortField(null, SortField.DOC, true));
			ftQuery.setSort(sort);
			resultList = ftQuery.list();
		} catch (Exception ex) {
			this.logAffectuException(ex, "查询出错");
		}
		long endTime = System.currentTimeMillis();
		sro.setTime((endTime - startTime) * (0.001));
		sro.setResult(resultList);
		return sro;
	}

	public String luceneSearch() {
		if ("task".equals(this.searchType)) {
			this.getSessionScope().put("indexAction_searchTasks",
					this.searchTasksBylucene());
			this.getSessionScope().remove("indexAction_searchUsers");
		} else if ("wike".equals(this.searchType)) {
			this.getSessionScope().put("indexAction_searchUsers",
					this.searchUserBySql());
			this.getSessionScope().remove("indexAction_searchTasks");
		} else {// 不指定搜索类型，默认为搜索任务
			this.setSearchType("task");
			this.luceneSearch();
		}
		return "/search/result.xhtml";
	}

	@SuppressWarnings("unchecked")
	public SearchResult<User> searchUserBySql() {
		SearchResult sro = new SearchResult<User>();
		List<User> users = new ArrayList<User>();
		long startTime = System.currentTimeMillis();
		try {
			users = em
					.createSQLQuery(
							"select u.* from User u where concat(u.nickName,ifnull(u.trueName,''),u.email,u.specialty,u.address) regexp :searchString")
					.addEntity("u", User.class).setParameter("searchString",
							this.getKeywords()).list();

		} catch (Exception ex) {
			this.logException(ex);
		}
		long endTime = System.currentTimeMillis();
		sro.setTime((endTime - startTime) * (0.001));
		sro.setResult(users);
		return sro;
	}

	public String getHighlighterText(String field, String inputText) {
		String limitedString = this.filterHtmlTags(inputText, 160);
		return LuceneUtil.getInstance().getHighlighterText(field, taskFields,
				limitedString, this.getKeywords());
	}

	public String getKeywords() {
		return keywords;
	}

	public void setKeywords(String searchString) {
		this.keywords = searchString;
	}

	public String getSearchType() {
		return searchType;
	}

	public void setSearchType(String searchType) {
		this.searchType = searchType;
	}
}
